﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="JsonReturn.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：JsonReturn.cs
// 创建时间：2014-10-28 15:30
// 修改时间：2014-10-28 15:30
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using WebCommon.SysConfig;

namespace WebCommon.Extensions
{
    using System;
    using System.Web.Mvc;

    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    using Newtonsoft.Json.Linq;

 

    [JsonObject(MemberSerialization.OptOut)]
    public class JsonReturn:ActionResult
    {
        [JsonProperty("url")]
        public string Url { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        [JsonProperty("data")]
        public object ModelData { get; set; }
        /// <summary>
        /// 状态代码
        /// </summary>
        [JsonProperty("code")]
        public string Code { get; set; }
        /// <summary>
        /// 代码消息
        /// </summary>
        [JsonProperty("ms")]
        public string CodeMs { get; set; }

        [JsonProperty("timestamp")]
        public string Timestamp { get; set; }
        /// <summary>
        /// 构造( Code = "0"; CodeMs = "正常";)
        /// </summary>
        public JsonReturn()
        {
            this.Code = "0";
            
            this.Url = "/";
            this.Timestamp = DateTime.Now.Ticks.ToString();
        
            //var json =   ContentType.json;

        }
        /// <summary>
        /// 构造( Code = "0"; CodeMs = "正常";)
        /// </summary>
        /// <param name="data">JSON数据模型</param>
        public JsonReturn(object data)
            : this()
        {
            this.ModelData = data;
 
        }

        /// <summary>
        /// 构造( Code = "0"; CodeMs = "正常";)
        /// </summary> 
        /// <param name="dataJson">JSON</param>
        public JsonReturn(string dataJson)
            : this()
        {
            this.ModelData = JObject.Parse(dataJson).ToObject<object>();
        }
     
       


        /// <summary>
        /// TOJson
        /// </summary>
        /// <returns></returns>
        private string ToJson()
        {
            IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
            timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
             
            return JsonConvert.SerializeObject(this, Newtonsoft.Json.Formatting.Indented, timeFormat);
        }

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Write(this.ToJson());
        }
    }
}